package org.agridata.akvisualization.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.agridata.akvisualization.doMain.entity.Livestock;
import org.agridata.akvisualization.doMain.dto.LivestockDto;
import org.agridata.akvisualization.doMain.vo.LivestockVo;
import org.agridata.akvisualization.mapper.LivestockMapper;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.LivestockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LivestockServiceImpl implements LivestockService {

    @Autowired
    private LivestockMapper mapper;

    @Override
    public Result importLivestock(List<Livestock> memberList, String dates, String cycle) {
        int successCount = 0;
        int failureCount = 0;
        try {
            List<Livestock> livestocks = mapper.selectLivestockByDates(dates, cycle);
            if (livestocks.size() > 0) {
                mapper.deleteLivestock(livestocks);
            }
            for (Livestock livestock : memberList) {
                try {
                    livestock.setXqDate(dates);
                    livestock.setXqCycle(cycle);
                    mapper.insertLivestock(livestock);
                    successCount++;
                } catch (Exception e) {
                    failureCount++;
                    e.printStackTrace();
                }
            }


            return Result.ok("成功：" + successCount + "条" + "，" + "失败" + failureCount + "条");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }
    }

    @Override
    public Result livestockListPage(LivestockDto livestockDto) {
        try {
            Page<LivestockVo> page = new Page<>(livestockDto.getPageNo(), livestockDto.getPageSize());
            Page<LivestockVo> livestockListPage = mapper.selectLivestockListPage(page, livestockDto);
            return Result.ok(livestockListPage);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("出现异常！");
        }
    }

    @Override
    public Result pricePerMensems() {
        try {
            Map<String, Map<String,List<String>>> map = new HashMap<>();
            List<LivestockVo> priceList =mapper.selectLivePricePerMensems();
            List<String> date = new ArrayList<>();
            List<String> zizgu = new ArrayList<>();
            List<String> shangpinzhu = new ArrayList<>();
            List<String> rouniu = new ArrayList<>();
            List<String> rouji = new ArrayList<>();
            List<String> rouyang = new ArrayList<>();
            for (LivestockVo livePigPrice : priceList) {
                String dates = livePigPrice.getXqDate();
                date.add(dates);
                if (livePigPrice.getZizgu()!=null){//仔猪
                    zizgu.add(livePigPrice.getZizgu());
                }
                if (livePigPrice.getShangpinzhu()!=null){//商品猪
                    shangpinzhu.add(livePigPrice.getShangpinzhu());
                }
                if (livePigPrice.getRouniu()!=null){//肉牛
                    rouniu.add(livePigPrice.getRouniu());
                }
                if (livePigPrice.getRouji()!=null){//肉鸡
                    rouji.add(livePigPrice.getRouji());
                }
                if (livePigPrice.getRouyang()!=null){//肉羊
                    rouyang.add(livePigPrice.getRouyang());
                }

            }
            Map<String,List<String>> zizguMap=new HashMap<>();
            Map<String,List<String>> shangpinzhuMap=new HashMap<>();
            Map<String,List<String>> rouniuMap=new HashMap<>();
            Map<String,List<String>> roujiMap=new HashMap<>();
            Map<String,List<String>> rouyangMap=new HashMap<>();

            zizguMap.put("price",zizgu);
            zizguMap.put("date",date);

            shangpinzhuMap.put("price",shangpinzhu);
            shangpinzhuMap.put("date",date);

            rouniuMap.put("price",rouniu);
            rouniuMap.put("date",date);

            roujiMap.put("price",rouji);
            roujiMap.put("date",date);

            rouyangMap.put("price",rouyang);
            rouyangMap.put("date",date);

            map.put("zizgu",zizguMap);
            map.put("shangpinzhu",shangpinzhuMap);
            map.put("rouniu",rouniuMap);
            map.put("rouji",roujiMap);
            map.put("rouyang",rouyangMap);
            return Result.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }
    }

    public void aaa(){
        try {
            Selector open = Selector.open();

            open.select();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
